package com.itheima.web.servlet;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet{
    private BrandService brandService=new BrandServiceImpl();
    //查询所有
    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("brand selectAll");
        //service查询
        List<Brand> brands=brandService.selectAll();
        //2.转为json
        String jsonStr = JSON.toJSONString(brands);
        //3.写数据
        response.setContentType("text/json;charset=utf-8"); //告知浏览器响应的数据是什么， 告知浏览器使用什么字符集进行解码
        response.getWriter().write(jsonStr);
    }

    //添加数据
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("brand add");
        //1.接收数据
        BufferedReader br=request.getReader();
        String params=br.readLine();//json字符串
        //转Brand对象
        Brand brand= JSON.parseObject(params,Brand.class);
        //2.调用service查询
        brandService.add(brand);
        //3.响应成功标识
        response.getWriter().write("success");
    }
    //批量删除
    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 接收数据 json  [1,2,3]
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串
        //转为 int[]
        int[] ids = JSON.parseObject(params, int[].class);
        //2. 调用service删除
        brandService.deleteByIds(ids);
        //3. 响应成功的标识
        response.getWriter().write("success");
    }
    public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 接收 当前页码 和 每页展示条数    url?currentPage=1&pageSize=5
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        //2. 调用service查询
        PageBean<Brand> pageBean = brandService.selectByPage(currentPage, pageSize);

        //2. 转为JSON
        String jsonString = JSON.toJSONString(pageBean);
        //3. 写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    /**
     * 分页条件查询
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */

    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 接收 当前页码 和 每页展示条数    url?currentPage=1&pageSize=5
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        // 获取查询条件对象
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串

        //转为 Brand
        Brand brand = JSON.parseObject(params, Brand.class);


        //2. 调用service查询
        PageBean<Brand> pageBean = brandService.selectByPageAndCondition(currentPage,pageSize,brand);

        //2. 转为JSON
        String jsonString = JSON.toJSONString(pageBean);
        //3. 写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    //修改数据
    public void update(int id) {}

    /**
     * 删除功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //1.接收浏览器发送的异步请求数据
        BufferedReader br = request.getReader();
        //2.读数据
        String _delId = br.readLine();
        System.out.println(_delId);
        int delId = Integer.parseInt(_delId);
        //4.调用service的add方法
        brandService.delete(delId);
        //5.给客户端浏览器一个添加成功的响应
        response.getWriter().write("success");
    }
    /**
     * 修改功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

        //1.接收浏览器发送的异步请求数据
        BufferedReader br = request.getReader();
        //2.读数据
        String jsonString = br.readLine();
        //3.将json数据转为Brand对象
        Brand brand = JSON.parseObject(jsonString, Brand.class);
        //4.调用service的add方法
        brandService.update(brand);
        //5.给客户端浏览器一个添加成功的响应
        response.getWriter().write("success");
    }

}
